﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace workshop3
{
    public partial class Formcoursecreate : Form
    {
        DataTable dt = new DataTable();
        DataTable dtreturn = new DataTable();
        public static int iconfirm = 0;
        public Formcoursecreate()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //DataTable dt = new DataTable();
            DataColumn dc1 = new DataColumn("Tasks");
            dt.Columns.Add(dc1);
            //dgvtask.DataSource = dt;
        }

        private void txtyear_KeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.KeyCode == Keys.Enter)
            {
                if (!ClassCreateXML.TextCheck(txtyear.ToString()))//check if the input is legal or not 
                {
                    return;
                }
                e.Handled = true;   //set the handle event is true, means that the event has been handled
                lblyear.Text = string.Empty;
                lblyear.Text = txtyear.Text.Trim().Replace("\n","");//ignore the enter key the user
                txtyear.Text = string.Empty;
                
            }
            
        }

        private void txtcoursename_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//check the user press the enter key
            {
                e.Handled = true;   
                lblname.Text = string.Empty;
                
                lblname.Text = txtcoursename.Text.Trim().Replace("\n","");
                txtcoursename.Text = string.Empty;
            }
        }

        private void txtsemester_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!ClassCreateXML.TextCheck(txtyear.ToString()))
                {
                    return;
                }
                e.Handled = true;
                lblsemester.Text = string.Empty;
               
                lblsemester.Text = txtsemester.Text.Trim().Replace("\n","");
                txtsemester.Text = string.Empty;
                
            }
        }

        private void btn_AddTask_Click(object sender, EventArgs e)
        {
            dt.Rows.Add(txttask.Text);
            dgvtask.DataSource = dt;
            txttask.Text = string.Empty;
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            if(ClassCreateXML.AddCourseXml(lblname.Text, lblyear.Text, lblsemester.Text, dt, dtreturn))
            {
                MessageBox.Show("Save successfully");
                this.Close();
            }
        }



        private void dgvtask_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            txttask.Text = dt.Rows[e.RowIndex][0].ToString();
        }

     

     

        private void dgvtask_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                dt.Rows.Remove(dt.Rows[e.RowIndex]);
            }
        }

        private void btn_AddAMS_Click(object sender, EventArgs e)
        {
            Formaddams ams = new Formaddams();
            ams.Owner = this;
            ams.ShowDialog();
            if (iconfirm == 1)
            {
                dtreturn = ams.dtams;
                MessageBox.Show("Add ams successfully!");
            }
        }

        private void btn_ViewAMS_Click(object sender, EventArgs e)
        {
            Formviewams ams = new Formviewams(dtreturn);
            ams.Show();

        }

        private void btnreset_Click(object sender, EventArgs e)
        {
            dt.Clear();
            dtreturn.Clear();
            lblname.Text = string.Empty;
            lblsemester.Text = string.Empty;
            lblyear.Text = string.Empty;
            //dgvtask.DataSource = null;
            
        }

        private void btn_user_Click(object sender, EventArgs e)
        {
            //Formmanagement user = new Formmanagement();
            //this.Hide();
            //user.Show();
        }
    }
}
